<?php
namespace app\admin\controller;

use app\admin\model\Category as CategoryModel;
use app\admin\validate\Category as CategoryValidate;
use app\common\exception\CategoryException;

class Category extends Base
{
    protected function _initialize()
    {
        //如果要检测token,就调用父类_initialize方法
        parent::_initialize();
    }
    /**
     * @description 获取所有商品分类信息
     */
    public function index()
    {
        $cates = CategoryModel::getRawAll();
        if(!$cates) {
            throw new CategoryException(['msg'=>'没有找到商品分类数据']);
        }
//        $res = arrayToTree($cates->toArray());
        return $this->ok($cates);
    }
    /**
     * @description 创建商品分类
     */
    public function save()
    {
        (new CategoryValidate())->scene('save')->goCheck();
        $params = input();
        // 父级分类id不为0
        if($params['parent_id'] != 0){
            $count = CategoryModel::where(['id'=>$params['parent_id']])->count('id');
            if(!$count) {
                throw new CategoryException(['msg'=>'父级分类不存在,请重新填写父级分类id']);
            }
            $params['is_root'] = 0;
        }else{
            //此时顶级分类
            $params['is_root'] = 1;
        }

        //$params['img']是url字符串

        //创建顶级分类,或者创建某分类的子分类
        CategoryModel::create($params, true);
        return $this->ok([], '创建商品分类成功');
    }
    /**
     * @description 查询商品分类信息
     */
    public function read($id)
    {
        (new CategoryValidate())->scene('read')->goCheck();
        $id = $this->request->param('id/d');

        $category = CategoryModel::with('topicImg')->find($id);
        if(empty($category)) {
            throw new CategoryException(['msg'=>'没有找到商品分类数据']);
        }

        $cates = CategoryModel::select()->toArray();
        $res = arrayToTree($cates, $id);
        $category = $category->toArray();
        $category['children'] = $res;

        return $this->ok($category);
    }
    /**
     * @description 更新商品分类信息
     */
    public function update($id)
    {
        (new CategoryValidate())->scene('update')->goCheck();
        $params = input();
        $id = input('put.id/d');
        $category = CategoryModel::get($id);
        if(empty($category)) {
            throw new CategoryException();
        }

        //父级分类id不为0
        if($params['parent_id']!=0){
            $count = CategoryModel::where(['id'=>$params['parent_id']])->count('id');
            if(!$count) {
                throw new CategoryException(['msg'=>'父级分类不存在,请重新填写父级分类id']);
            }
            $params['is_root'] = 0;
        }else{
            //此时顶级分类
            $params['is_root'] = 1;
        }

        //$params['img']是url字符串

        CategoryModel::update($params, ['id'=>$id], true);
        return $this->ok([], '更新商品分类成功');
    }

    /**
     * @description 删除商品分类信息
     */
    public function delete($id)
    {
        (new CategoryValidate())->scene('delete')->goCheck();
        $id = input('delete.id/d');
        $count = CategoryModel::where(['parent_id'=>$id])->count('id');
        if($count){
            //有子分类,不能直接删除
            throw new CategoryException(['msg'=>'该分类下有子分类,不能直接删除,如果想删除,请先删除子分类']);
        }
        //没有子分类,可以删除,此处是软删除
        CategoryModel::destroy($id, false);
        return $this->ok([], '删除成功');
    }
    /**
     * @description 分类图片上传接口(单图片)
     */
    public function upload()
    {
        //上传成功,返回图片url地址
        return $this->upload_img('category');
    }
    /**
     * @description 分类图片上传接口(多图片)
     */
    public function uploads()
    {
        //上传成功,返回数组
        return $this->upload_imgs('category');
    }


}